package com.norbitltd.guavaleaves.java.io;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.commons.io.FileUtils;
import com.google.common.base.Function;

public class FileFunctions {
	
	public static Function<File, String> getCanonicalPath() {
		return new Function<File, String>() {
			public String apply(File input) {
				try {
					return input.getCanonicalPath();	
				} catch(IOException e) {
					return null;
				}
				
			}
		};
	}
	
	public static Function<File, String> getName() {
		return new Function<File, String>() {
			public String apply(File input) {
				return input.getName();
			}
		};
	}

	public static Function<File, String> getAbsolutePath() {
		return new Function<File, String>() {
			public String apply(File input) {
				return input.getAbsolutePath();
			}
		};
	}
	
	public static Function<File, String> readFileToString() {
		return new Function<File, String>() {
			public String apply(File input) {
				try {
					return FileUtils.readFileToString(input);	
				} catch(Exception e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
	
	public static Function<File, String> readFileToString(final Charset encoding) {
		return new Function<File, String>() {
			public String apply(File input) {
				try {
					return FileUtils.readFileToString(input, encoding);	
				} catch(Exception e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
	
	public static Function<File, String> readFileToString(final String encoding) {
		return new Function<File, String>() {
			public String apply(File input) {
				try {
					return FileUtils.readFileToString(input, encoding);	
				} catch(Exception e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
	
	public static Function<File, List<String>> readLines() {
		return new Function<File, List<String>>() {
			public List<String> apply(File input) {
				try {
					return FileUtils.readLines(input);	
				} catch(Exception e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
	
	public static Function<File, List<String>> readLines(final Charset encoding) {
		return new Function<File, List<String>>() {
			public List<String> apply(File input) {
				try {
					return FileUtils.readLines(input, encoding);	
				} catch(Exception e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
	
	public static Function<File, List<String>> readLines(final String encoding) {
		return new Function<File, List<String>>() {
			public List<String> apply(File input) {
				try {
					return FileUtils.readLines(input, encoding);	
				} catch(Exception e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
}
